package com.example.spring_transaction.service;

import com.example.spring_transaction.mapper.UserInfoMapper;
import com.example.spring_transaction.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @className: UserService
 * @author: 芃
 * @description:
 * @date: 2024/12/30 17:41
 */
@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private LogService logService;

    public Integer addUser(String userName,String password){
        return userInfoMapper.insert(userName,password);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Integer addUser2(String userName,String password){
        Integer result = userInfoMapper.insert(userName,password);
        Integer result2 = logService.addLog(userName,null);
        if (result == 1 && result2 == 1){
            return 1;
        }else {
            return 0;
        }
    }


    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Boolean login(String userName,String password){
        UserInfo userInfo = userInfoMapper.selectByName(userName);
        if (userInfo != null && userInfo.getPassword().equals(password)){
            return true;
        }else {
            return false;
        }
    }

}
